/*********** NOTES HEADER *********** 

DESCRIPTION: This program creates the merger-by-merger figures from the estimates


************************************/ 

set more off
capture log close
clear all

global origData "K:\BE\1257\Projects\Medicare Claims\HospRetro\Drafts\Health_Outcomes\FigureCreation\PSPatients"
global logs "K:\BE\1257\Projects\Medicare Claims\HospRetro\Drafts\Health_Outcomes\FigureCreation\PSPatients"
global dataIn "K:\BE\1257\Projects\Medicare Claims\HospRetro\Drafts\Health_Outcomes\FigureCreation\PSPatients"
global dataOut "K:\BE\1257\Projects\Medicare Claims\HospRetro\Drafts\Health_Outcomes\FigureCreation\PSPatients"
global Output "K:\BE\1257\Projects\Medicare Claims\HospRetro\Drafts\Health_Outcomes\FigureCreation\PSPatients"
global dpath "K:\BE\1257\Projects\Medicare Claims\HospRetro\Drafts\Health_Outcomes\FigureCreation\PSPatients"
global skapath "K:\BE\1257\Projects\Medicare Claims\HospRetro\Drafts\Health_Outcomes\FigureCreation\PSPatients"

	use mbm_death_small.dta 

	capture rename rnglaucoma_ coeff_labels
	keep coeff_ mem*
	 replace coeff=regexr(coeff,"oc_glaucoma:","")
	 
	preserve
	keep coeff
	gen id=_n+1
	sort id
	tempfile clabels
	save `clabels', replace

	restore
	drop coeff
	gen id=_n
	merge 1:1 id using `clabels'
	keep if mem_beta_glaucoma~=.
	replace coeff_labels="pmVMP0" if _merge==1
	drop _merge id
	order coeff

		foreach i of varlist mem* {
			local j = regexr("`i'","mem_","")
			rename `i' `j'
		} 

	reshape long beta_ sesq_, i(coeff) j(depvar) str
		 drop if inlist(depvar, "ami_1", "ami_2", "ami_3","death_2","death_3")
		 replace depvar=regexr(depvar,"[\_][0-9]","")
		 replace depvar=regexr(depvar,"_","")
		 replace coeff=regexr(coeff,".idpost","")
		destring coeff, replace
		 
		 rename beta_ beta 
		 rename sesq_ sesq
		 
		replace depvar=trim(depvar)


		gen dvar="Acute heart condition separately by merger" if depvar=="acuteicd8"
		 replace dvar="AMI separately by merger" if depvar=="ami"                                         
		 replace dvar="Congestive heart failure separately by merger" if depvar=="chf"
		 replace dvar="Mortality separately by merger" if depvar=="death"                                         
		 replace dvar="Diabetes complications separately by merger" if depvar=="diabcomp" 
		 replace dvar="Asymptomatic diabetes complications separately by merger" if depvar=="diabcomp1"                                         
		 replace dvar="Symptomatic diabetes complications separately by merger" if depvar=="diabcomp2"                                         
		 replace dvar="Glaucoma separately by merger" if depvar=="glaucoma"
		 replace dvar="Ischemic Heart Disease separately by merger" if depvar=="ihd"
		 replace dvar="Malignant Hypertension separately by merger" if depvar=="malighyp"                                         
		 
		gen l95= beta - sqrt(sesq)*2
		gen u95= beta + sqrt(sesq)*2

		  label var b "Post-merger effect"
		  label var l95  " "
		  label var u95  "95% CI"

		  drop if l95 < -1.05
		  drop if u95 > 1.05
		  
		  sort dvar b
		  by dvar: gen x=_n
		  label var x "Merger"

	graph set window fontface "Times New Roman"
	graph set ps fontface "Times New Roman"

	*Diabetes 
		 foreach i in diabcomp1 diabcomp2 glaucoma {
		   preserve
		   keep if depvar=="`i'"
			local num = _N
			levelsof dvar if depvar=="`i'", local(k)
			twoway  || scatter b x if coeff_labels!="pmVMP0", mcolor(blue) || scatter b x if coeff_labels=="pmVMP0", mcolor(blue) msymbol(circle_hollow) || rspike u95 l95 x ,  graphregion(color("white")) ytitle(Marginal Effect)  ///
			 xlabel(none) || rcap u95 u95 x || rcap l95 l95 x , ///
			 legend(order(1 "Post-merger effect" 4 "95% C.I.") ) xtick(1(1)`num')  
			 graph export "./PSPatients_MgrbyMgr_`i'.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace
		restore
		   }

	*Hypertension	   
		  foreach i in acuteicd8  ami  ihd  {
		   preserve
		   keep if depvar=="`i'"
			local num = _N
			levelsof dvar if depvar=="`i'", local(k)
			twoway  || scatter b x if !inlist(coeff_labels,"pmVMP0", "pmVMP26", "pmVMP27", "pmVMP28", "pmVMP41", "pmVMP42","pmVMP43","pmVMP47"), mcolor(blue)|| scatter b x if inlist(coeff_labels,"pmVMP26", "pmVMP27", "pmVMP28", "pmVMP41", "pmVMP42","pmVMP43","pmVMP47"), mcolor(blue) msymbol(square) ///
			|| scatter b x if coeff_labels=="pmVMP0", mcolor(blue) msymbol(circle_hollow) || rspike u95 l95 x , lcolor(black)  graphregion(color("white")) ytitle(Marginal Effect)  ///
			 xlabel(none) || rcap u95 u95 x || rcap l95 l95 x , ///
			 legend(order(1 "Post-merger effect" 5 "95% C.I.") ) xtick(1(1)`num')  
			 graph export "./PSPatients_MgrbyMgr_`i'.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace
		restore
		   }
    
	*Mortality 
		 foreach i in  death {
		   preserve
		   keep if depvar=="`i'"
		   keep if beta+sesq+l95+u95~=0
		   local num = _N
		   drop x
		   sort dvar b
		   by dvar: gen x=_n
		   label var x "Merger"
		   levelsof dvar if depvar=="`i'", local(k)
			twoway || scatter b x if coeff_labels!="pmVMP0", mcolor(blue) || scatter b x if coeff_labels=="pmVMP0", mcolor(blue) msymbol(circle_hollow) || rspike u95 l95 x ,  graphregion(color("white")) ytitle(Marginal Effect)  ///
			 xlabel(none) || rcap u95 u95 x || rcap l95 l95 x , ///
			 legend(order(1 "Post-merger effect" 4 "95% C.I.") ) xtick(1(1)`num') 
			 graph export "./PSPatients_MgrbyMgr_`i'.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace
		restore
		   }
